
#include "User_App.h"
#include "bsp_port.h"
#include "bsp_Timer.h"
#include "stdint.h"
#include "modbus.h"
#include "bsp_ADC.h"

uint16_t ADC_Test_Data_Temp; // NTC温度
uint8_t WaterPlace;          // 水位0、1、2

bsp_TIM_t Ti_RUN_LED;
bsp_TIM_t Ti_USART1;
bsp_TIM_t Ti_ADC;
bsp_TIM_t Ti_ADC_DATA;

Modbus_t modbus = {0};
uint8_t ModbusDataBuf[256];

uint16_t mb_Value;

void Ti_RUN_LED_event(void);
void Ti_USART1_event(void);
static void MB_SendData(uint8_t *data, uint16_t datalen);
static MB_ERR_Typedef ModbusWriteReg(uint16_t StartAddr, uint16_t *data, uint16_t Datalen);
static MB_ERR_Typedef ModbusReadReg(uint16_t StartAddr, uint16_t *data, uint16_t Datalen);

void User_App(void)
{
#if 0

  // LED_Run_Debug();
  // bsp_Ti_Debug();
  bsp_Adc_Debug();
  // bsp_uart_Debug();

#else
  bsp_Adc_Init(&ADC_OBEY);

  Ti_obj_New(&Ti_RUN_LED, 500, Ti_RUN, Ti_RUN_LED_event);
  Ti_obj_New(&Ti_ADC, 20, Ti_RUN, Ti_ADC_Function);
  Ti_obj_New(&Ti_ADC_DATA, 20 * ADC_OBJ_DATA_NUM, Ti_RUN, Ti_ADC_DATA_Function);

  MB_SetLocalAddr(&modbus, 0x01);
  MB_SetBuad(&modbus, 9600);
  MB_AddSendBytes(&modbus, MB_SendData);                                    // 添加串口发送函数
  MB_AddGetTimestamp(&modbus, get_Timestampus);                             // 添加定时获取时间，防止超时
  MB_SetReadRegs_UserFun(&modbus, ModbusReadReg);                           // 设置用户读取函数
  MB_SetWriteRegs_UserFun(&modbus, ModbusWriteReg);                         // 设置用户写入函数
  MB_AddRxDataBuf(&modbus, (uint32_t)ModbusDataBuf, sizeof(ModbusDataBuf)); // 添加接收缓存区

  while (1)
  {
    Modbus_Parse(&modbus); // modbus

    bsp_Ti_All_Handle();
  }
#endif
}

void Ti_RUN_LED_event(void)
{
  LED_RUN_TOGG;
}
void Ti_USART1_event(void)
{
  printf("hallo,world\n");
}
void LED_Run_Debug(void)
{
  while (1)
  {
    LED_RUN_TOGG;
    // LL_mDelay(500);
  }
}

// 串口发送函数
static void MB_SendData(uint8_t *data, uint16_t datalen)
{
  bsp_uart_SendData(BSP_UART1, data, datalen);
}
// 设置用户写入函数
static MB_ERR_Typedef ModbusWriteReg(uint16_t StartAddr, uint16_t *data, uint16_t Datalen)
{
  uint16_t Reg_Index = 0;
  while (Reg_Index < Datalen)
  {
    switch (StartAddr + Reg_Index)
    {
    case MB_REG_TUBE_CLOSE_START: // 开始封口
    {
      mb_Value = data[0];
    }
      Reg_Index += 1;
      data += 1;
      break;
    default:
      return MB_DATA_ADDR_ERR;
    }
  }
  return MB_OK;
}
// 设置用户读取函数
static MB_ERR_Typedef ModbusReadReg(uint16_t StartAddr, uint16_t *data, uint16_t Datalen)
{
  uint16_t Reg_Index = 0;
  while (Reg_Index < Datalen)
  {
    switch (StartAddr + Reg_Index)
    {
    case MB_REG_TUBE_CLOSE_START: // 开始封口
    {
      data[0] = 0x0001;
    }
      Reg_Index += 1;
      data += 1;
      break;
    case MB_REG_GET_NOW_TEMP:
    {
      data[0] = ADC_Test_Data_Temp;
    }
      Reg_Index += 1;
      data += 1;
      break;
    case MB_REG_WATER_PLACE:
    {
      data[0] = 0;
      if (WATER_PLACE_01_ISIN == 0)
      {
        data[0] = 1;
      }
      if (WATER_PLACE_02_ISIN == 0)
      {
        data[0] = 2;
      }
    }
      Reg_Index += 1;
      data += 1;
      break;
    default:
      return MB_DATA_ADDR_ERR;
    }
  }
  return MB_OK;
}
